package com.gs.study.service.impl;




import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gs.study.domain.p.User;
import com.gs.study.domain.p.UserDao;
import com.gs.study.service.EhcacheService;

/** 
 * @author gaoshang 
 * @date 创建时间：2018年5月17日 上午11:33:21 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */
@Service
@Transactional
public class EhcacheServiceImpl implements EhcacheService{
	@Autowired
	private UserDao userDao;
	
	 
	    //这里的单引号不能少，否则会报错，被识别是一个对象
	    private static final String CACHE_KEY = "'user'";
	    private static final String DEMO_CACHE_NAME = "users";
	 
	 
	    //删除用户数据
	    @CacheEvict(value = DEMO_CACHE_NAME,key = "'user_'+#id")//这是清除缓存
	    public void delete(Long uuid)throws Exception{
	        userDao.delete(uuid);
	    }
	 
	    //更新用户数据
	    @CachePut(value = DEMO_CACHE_NAME,key = "'user_'+#user.getId()")
	    public User update(User user) throws Exception{
	        userDao.update(user);
	        return user;
	    }
	 
	    //查找用户数据
	    @Cacheable(value=DEMO_CACHE_NAME,key="'user_'+#id")
	    public User findByUuid(Long uuid)throws Exception{
	        //若找不到缓存将打印出提示语句
	        System.err.println("没有走缓存！"+uuid);
	        return userDao.findByUuid(uuid);
	    }
	 
	    //保存用户数据
	    @CacheEvict(value=DEMO_CACHE_NAME,key=CACHE_KEY)
	    public User save(User user)throws Exception{
	    	userDao.save(user);
	         return user;
	    }

		@Override
		public List<User> findAllUser()throws Exception {
			// TODO Auto-generated method stub
			return null;
		}


}
